<?php

namespace app\data\controller\control;

use app\data\model\GoodsItemModel;
use app\data\model\GoodsStockModel;
use app\data\model\GoodsRetrievalModel;
use app\data\model\SendModel;
use app\data\model\StorehouseModel;
use think\admin\Controller;
use think\admin\extend\CodeExtend;
use think\facade\Db;

/**
 * 仓库分类管理
 * Class Goods
 * @package app\data\controller\shop
 */
class Storehouse extends Controller
{
    /**
     * 绑定分类表
     * @var string
     */
    private $table = 'SnStorehouse';

    /**
     * 最大分类等级
     * @var integer
     */
    protected $cateLevel;

    /**
     * 仓库分类管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->title = '仓库分类管理';
        $query = $this->_query($this->table);
        // 加载对应分类
        $this->type = $this->request->get('type', 'index');
        if ($this->type === 'index') $query->where(['deleted' => 0]);
        elseif ($this->type === 'recycle') $query->where(['deleted' => 1]);
        else $this->error("无法加载 {$this->type} 分类列表！");
        // 列表排序并显示
        $query->like('code,name')->like('cateids');
        $query->equal('status')->order('sort desc,id desc')->page();
    }

    /**
     * 分类列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _page_filter(array &$data)
    {

    }

    /**
     * 添加仓库分类
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        $this->mode = 'add';
        $this->title = '添加仓库分类';
        $this->_form($this->table, 'form', 'id');
    }

    /**
     * 编辑仓库分类
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $this->mode = 'edit';
        $this->title = '编辑仓库分类';
        $this->_form($this->table, 'form', 'id');
    }


    /**
     * 表单分类处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        if ($this->request->isPost()){
            if (empty($data['code'])) {
                $data['code'] = CodeExtend::uniqidNumber(20, 'G');
            }
            $where = ['deleted' => 0,'code'=>$data['code']];
            if(isset($data['id'])){
                $code = StorehouseModel::where(['deleted' => 0,'id'=>$data['id']])->value('code');
                if($code != $data['code']){
                    $count = StorehouseModel::where($where)->count();
                    if($count>0)$this->error('仓库编号已存在！');
                }
            }else{
                $count = StorehouseModel::where($where)->count();
                if($count>0)$this->error('仓库编号已存在！');
            }
            if (empty($data['name'])) $this->error('仓库名称不能为空！');
            if (empty($data['code'])) $this->error('仓库编号不能为空！');
        }
    }

    /**
     * 表单结果处理
     * @param boolean $result
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_result(bool $result)
    {
        if ($result && $this->request->isPost()) {
            $this->success('仓库保存成功！');
        }
    }

    /**
     * 删除仓库分类
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function remove()
    {
        $this->_save($this->table, $this->_vali([
            'deleted.in:0,1'  => '状态值范围异常！',
            'deleted.require' => '状态值不能为空！',
        ]), 'id');
    }

}